/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.eventos.bc;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.cip.esymons.eventos.be.Ambiente;

/**
 *
 * @author OscarA
 */
public class PruebaDelegate {

    AmbienteFacadeLocal local;

    public PruebaDelegate() {
        local = lookupAmbienteFacade();
    }

    public Ambiente buscarAmbiente(Integer id) {
        try{
            return local.find(id);
        }catch(Exception e){
            return new Ambiente();
        }
    }

    public List<Ambiente> listarAmbienteXLike(String ambienteBusqueda) throws Exception {
        try {
            return local.listarXLike(ambienteBusqueda);
        } catch (Exception e) {
             local = lookupAmbienteFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }

    private AmbienteFacadeLocal lookupAmbienteFacade() {
        try {
            Context c = new InitialContext();
            return (AmbienteFacadeLocal) c.lookup("java:comp/env/AmbienteFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public List<Ambiente> listarAmbiente() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupAmbienteFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }

    public List<Ambiente> listarAmbientexComiteLocal(Long id) throws Exception {
        try {
            return local.ambientesxComiteLocal(id);
        } catch (Exception e) {
            local = lookupAmbienteFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }

    public void crearAmbiente(Ambiente a) throws Exception {
        try {
            local.create(a);
        } catch (Exception e) {
            local = lookupAmbienteFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }
     public void actualizarAmbiente(Ambiente a) throws Exception {
        try {
            local.edit(a);
        } catch (Exception e) {
            local = lookupAmbienteFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }
}
